package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.sale.RecyclingRoleAdapter
import com.lykj.llw.box.data.bean.response.RecyclingResponse
import com.lykj.llw.box.databinding.ActRecyclingSmallAccountBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 回收小号页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/20-15:07
 */
class RecyclingSmallAccountActivity : BaseActivity() {
    lateinit var binding: ActRecyclingSmallAccountBinding

    private val mAdapter = RecyclingRoleAdapter()
    private val mData = ArrayList<RecyclingResponse.DataDTO>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActRecyclingSmallAccountBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar
            .with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()

        obtainRecyclingList()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "回收小号"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initRCV()
    }

    private fun initRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvRecycling.layoutManager = linearLayoutManager
        binding.rcvRecycling.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_15dp, resources.newTheme()
            )
        )
        binding.rcvRecycling.addItemDecoration(dividerItemDecoration, 0)
        mAdapter.data = mData
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val intent =
                Intent(
                    this@RecyclingSmallAccountActivity,
                    RecyclingSmallAccountDetailActivity::class.java
                )
            intent.putExtra("bean", mData[position])
            startActivity(intent)
        }
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无数据"
        mAdapter.setEmptyView(emptyView)
        binding.rcvRecycling.adapter = mAdapter
    }

    //获取回收小号列表
    private fun obtainRecyclingList() {
        RetrofitManager.instance.obtainRecyclingList(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@RecyclingSmallAccountActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, RecyclingResponse::class.java)

                mData.clear()
                if (!bean.data.isNullOrEmpty()) {
                    mData.addAll(bean.data)
                }
//                else {
//                    ToastUtils.showShort(this@RecyclingSmallAccountActivity, "暂无可回收角色数据")
//                }
                mAdapter.notifyDataSetChanged()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@RecyclingSmallAccountActivity, it)
                }
            }
        })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.lltRecyclingRecord.clickWithTrigger {
            startActivity(Intent(this, RecyclingRecordActivity::class.java))
        }

        binding.lltRecyclingDesc.clickWithTrigger {
            startActivity(Intent(this, RecyclingDescActivity::class.java))
        }

        binding.srfRecycling.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                obtainRecyclingList()
                refreshLayout.finishRefresh(350)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                refreshLayout.finishLoadMore(350)
            }
        })
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}